package com.meixiang.fragment.service;


import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.homes.service.CosmetologistActivity;
import com.meixiang.adapter.service.CosmetologistAdapter;
import com.meixiang.entity.services.Beautician;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseFragment;
import com.meixiang.recyclerview.OnItemClick;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 预约内的美容师列表
 * xiejingwen
 */
public class CosmetologistFragment extends BaseFragment implements OnRefreshListener,OnLoadMoreListener{

    private View view;
    @Bind(R.id.swipe_target)
    RecyclerView swipeTarget;
    @Bind(R.id.refresh)
    SwipeToLoadLayout refresh;
    private CosmetologistAdapter adapter;
    private Beautician beautician;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_project, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        refresh.setOnRefreshListener(this);
        refresh.setOnLoadMoreListener(this);
        //自动上拉
        swipeTarget.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (!ViewCompat.canScrollVertically(recyclerView, 1)) {
                        refresh.setLoadingMore(true);
                    }
                }
            }
        });
        swipeTarget.setLayoutManager(new LinearLayoutManager(getActivity()));
        //swipeTarget.addItemDecoration(new RecycleViewDivider(getActivity(),RecycleViewDivider.VERTICAL_LIST));
        adapter=new CosmetologistAdapter(this);
        adapter.setOnItemClickListener(new OnItemClick() {
            @Override
            public void onItemClick(View view, int position) {
                Bundle bundle=new Bundle();
                bundle.putString("beauticianId",adapter.data.get(position).getBeauticianId());
                startActivity(bundle,CosmetologistActivity.class);
            }
        });
        swipeTarget.setAdapter(adapter);
    }

    @Override
    protected void setData() {
        showLoading();
        getData(1,true);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
    private void getData(int current,final boolean isDownPull){
        HttpParams params=new HttpParams();
        params.put("pageNo",current+"");
        HttpUtils.post(Config.BEAUTICIAN_LIST, params,new HttpCallBack(activity) {
            @Override
            public void onSucceed (JSONObject result, String code, String message){
                    if (isDownPull) {
                        adapter.clearData();
                    }
                    beautician = AbJsonUtil.fromJson(result.toString(), Beautician.class);
                    if (beautician!=null&&beautician.getList()!=null&&beautician.getList().size() > 0) {
                        removeStatus();
                        adapter.addData(beautician.getList());
                    } else {
                        showNoData("暂无数据");
                    }
            }

            @Override
            public void onError (String code, String message){
                //AbToastUtil.showToast(activity, message);
                showNoData(message, "重新加载", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showLoading();
                        getData(1,true);
                    }
                });
            }

            @Override
            public void onAfter ( boolean isFromCache, JSONObject jsonObject, Call call, Response
            response, Exception e){
                super.onAfter(isFromCache, jsonObject, call, response, e);
                onComplete(refresh);
            }
        });
    }

    @Override
    public void onLoadMore() {
        int pageNo=Integer.parseInt(beautician.getPageNo());
        int  totalPage=Integer.parseInt(beautician.getTotalPage());
        if (pageNo<totalPage){
            pageNo++;
            getData(pageNo,false);
        }else{
            onComplete(refresh);
            AbToastUtil.showToast(activity,"已无更多数据");
        }

    }

    @Override
    public void onRefresh() {
        getData(1,true);
    }
}
